var layout_settings = new Array();
function getCSS(){
	var cssString = "/*MenuBuilderVars\n";
	layout_settings['menuOrientation'] = document.getElementById("menuOrientation").value;
	cssString += "menuOrientation:"+layout_settings['menuOrientation']+"\n";
	layout_settings['menuCurrentStyle']=document.getElementById('menuCurrentStyle').checked?1:0;
	cssString += "menuCurrentStyle:"+layout_settings['menuCurrentStyle']+"\n";
	layout_settings['submenuIndicatorType']=document.getElementById("submenuIndicatorType").value;
	cssString += "submenuIndicatorType:"+layout_settings["submenuIndicatorType"]+"\n";
	layout_settings['submenuIndicatorVar']=document.getElementById("submenuIndicatorVar").value;
	cssString += "submenuIndicatorVar:"+layout_settings["submenuIndicatorVar"]+"\n";
	//cssString += "menu_distance:"+document.getElementById('menu_distance').value+"\n";
	//layout_settings['menu_distance']=document.getElementById('menu_distance').value;
	cssString += getCSSValues('main');
	cssString += getCSSValues('activemain');
	if(layout_settings['menuCurrentStyle']) cssString += getCSSValues('currentmain');
	cssString += getCSSValues('sub');
	cssString += getCSSValues('activesub');
	if(layout_settings['menuCurrentStyle']) cssString += getCSSValues('currentsub');
	cssString += "MenuBuilderVars*/\n\n";
	if(layout_settings['menuOrientation']=='vertical') cssString += getVerticalCss().replace("--","");
	else cssString += getHorizontalCSS().replace("--","");
	return cssString;
}
function getCSSValues(id){
	var cssString = "";
	cssString += addLayoutTree(id, ['_width']);
	cssString += addLayoutTree(id, ['_height']);
	cssString += addLayoutTree(id, ['_padding']);
	cssString += addLayoutTree(id, ['_distance']);
	cssString += addLayoutTree(id, ['_border_size', '_border_type', '_border_color']);
	cssString += addLayoutTree(id, ['_background_color']);
	cssString += addLayoutTree(id, ['_background_image_url', '_background_image_offset_y', '_background_image_offset_x', '_background_image_repeat']);
	cssString += addLayoutTree(id, ['_font_size', '_font_color', '_font_weight', '_font_decoration', '_font_family']);
	cssString += addLayoutTree(id, ['_text_alignment']);
	cssString += addLayoutTree(id, ['_opacity']);
	return cssString;
}
function addLayoutTree(id, fields){
	var cssString = "";
	var checkerID = id+fields[0]+'_checker';
	if(document.getElementById(checkerID) && document.getElementById(checkerID).checked){
		for(var i=0; i<fields.length; i++){
			var svalue = document.getElementById(id+fields[i]).value;
			cssString+=id+fields[i]+":"+svalue+"\n";
			layout_settings[id+fields[i]]=svalue;
		}
	}
	return cssString;
}
function getLayoutValue(id, indication){
	if(layout_settings[id+indication]){
		return layout_settings[id+indication];
	}else if(layout_settings['main'+indication]){
		return layout_settings['main'+indication];
	}else{
		return '';
	}
}
function getHorizontalCSS(){
	var cssString = "";
	var mainWidth = cssAdd(getLayoutValue('main','_width'), getLayoutValue('main','_padding'),getLayoutValue('main','_padding'), getLayoutValue('main','_border_size'),getLayoutValue('main','_border_size'));
	var subWidth = cssAdd(getLayoutValue('sub','_width'), getLayoutValue('sub','_padding'),getLayoutValue('sub','_padding'), getLayoutValue('sub','_border_size'),getLayoutValue('sub','_border_size'));
	cssString+=".menulist, .menulist  ul {\n";
	cssString+="\tmargin: 0;\n";
	cssString+="\tpadding: 0;\n";
	cssString+="\tlist-style: none;\n";
	cssString+="\tz-index: 10001;\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist {\n";
	//cssString+="\twidth: "+mainWidth+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul {\n";
	cssString+="\tdisplay: none;\n";
	cssString+="\tposition: absolute;\n";
	cssString+="\ttop: 5px; margin-top: "+getLayoutValue('main', '_height')+";\n";
	cssString+="\tleft: -1px;\n";
	cssString+="\twidth: "+getLayoutValue('sub', '_width')+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul ul {\n";
	cssString+="\tmargin-top: 0;\n";
	cssString+="\tleft: "+getLayoutValue('sub', '_width')+";\n"; // maybe calculatiion + padding + border
	cssString+="\ttop:-"+getLayoutValue('sub','_distance')+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist li {\n";
	cssString+="\tfloat: left;\n";
	cssString+="\tdisplay: block;\n";
	cssString+="\tposition: relative;\n";
	cssString+="\tmargin-right: "+layout_settings['main_distance']+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul li {\n";
	cssString+="\tfloat: none;\n";
	cssString+="\tmargin-right: 0;\n";
	cssString+="\tmargin-top:"+getLayoutValue('sub','_distance')+";\n";
	cssString+="\tmargin-bottom: -1px;\n";
	//cssString+="\tborder: "+getLayoutValue('sub','_border_size')+" "+getLayoutValue('sub','_border_type')+" "+getLayoutValue('sub','_border_color')+";\n";
	cssString+="}\n";
	cssString+=".menulist ul>li:last-child {\n";
	cssString+="\tmargin-bottom: 1px; /* Mozilla fix */\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist a {\n";
	cssString+="\tdisplay: block;\n";
	//cssString+="\tborder: 0px;\n";
	cssString+=getLayoutPerModus('main');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul a {\n";
	cssString+="\tdisplay: block;\n";
	cssString+=getLayoutPerModus('sub');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist a:hover, .menulist a.highlighted:hover, .menulist a:focus {\n";
	cssString+=getLayoutPerModusCommon('activemain');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul a:hover, .menulist ul a.highlighted:hover, .menulist ul a:focus {\n";
	cssString+=getLayoutPerModusCommon('activesub');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist a.highlighted {\n";
	cssString+=getLayoutPerModusCommon('activemain');
	cssString+="}\n";
	cssString+=".menulist ul a.highlighted {\n";
	cssString+=getLayoutPerModusCommon('activesub');
	cssString+="}\n";
	if(layout_settings['menuCurrentStyle']){
		cssString+=".menulist a.menu-current-page {\n";
		cssString+="\tdisplay: block;\n";
		cssString+=getLayoutPerModus('currentmain');
		cssString+="}\n";
		cssString+=".menulist ul a.menu-current-page {\n";
		cssString+="\tdisplay: block;\n";
		cssString+=getLayoutPerModus('currentsub');
		cssString+="}\n";
	}
	cssString+="\n";
	cssString+=".menulist a .subind {\n";
	cssString+="\tdisplay: none;\n";
	cssString+="}\n";
	cssString+=".menulist ul a .subind {\n";
	cssString+="\tdisplay: block;\n";
	cssString+="\tfloat: right;\n";
	cssString+=getFontCss('sub');
	cssString+="}\n";
	cssString+="\n";
	return cssString;
}
function getLayoutPerModus(id){
	var cssString = "";
	cssString+="\tpadding:"+getLayoutValue(id,'_padding')+";\n";
	cssString+=getLayoutPerModusCommon(id);
	return cssString;
}
function getLayoutPerModusCommon(id){
	var cssString = "";
	cssString+="\twidth: "+cssSubstractSpecial(cssSubstractSpecial(getLayoutValue(id, '_width'), getLayoutValue(id, '_padding'), 'width'), getLayoutValue(id, '_border_size'), 'width')+";\n";
	cssString+="\theight:"+cssSubstractSpecial(cssSubstractSpecial(getLayoutValue(id, '_height'),getLayoutValue(id, '_padding'), 'height'), getLayoutValue(id, '_border_size'), 'height')+";\n";
	cssString+="\tborder: "+getLayoutValue(id,'_border_size')+" "+getLayoutValue(id,'_border_type')+" "+getLayoutValue(id,'_border_color')+";\n";
	cssString+="\tbackground-color:"+getLayoutValue(id,'_background_color')+";\n";
	cssString+="\tbackground-image:url("+getLayoutValue(id,'_background_image_url')+");\n";
	cssString+="\tbackground-position:"+getLayoutValue(id,'_background_image_offset_x')+" "+getLayoutValue(id,'_background_image_offset_y')+";\n";
	cssString+="\tbackground-repeat:"+getLayoutValue(id,'_background_image_repeat')+";\n";
	cssString+=getFontCss(id);
	cssString+="\ttext-align:"+getLayoutValue(id,'_text_alignment')+";\n";
	cssString+="\topacity:"+getLayoutValue(id,'_opacity')+";\n";
	cssString+="\tfilter: alpha(opacity="+getLayoutValue(id,'_opacity')*100+");\n";
	return cssString;
}
function getFontCss(id){
	var cssString = "";
	cssString+="\tcolor:"+getLayoutValue(id,'_font_color')+";\n";
	cssString+="\tfont-weight:"+getLayoutValue(id,'_font_weight')+";\n";
	cssString+="\tfont-size:"+getLayoutValue(id,'_font_size')+";\n";
	cssString+="\ttext-decoration:"+getLayoutValue(id,'_font_decoration')+";\n";
	cssString+="\tfont-family:"+getLayoutValue(id,'_font_family')+";\n";
	return cssString;
}
function getVerticalCss(){
	var cssString = "";
	var mainWidth = cssAdd(getLayoutValue('main','_width'), getLayoutValue('main','_padding'),getLayoutValue('main','_padding'), getLayoutValue('main','_border_size'),getLayoutValue('main','_border_size'));
	var subWidth = cssAdd(getLayoutValue('sub','_width'), getLayoutValue('sub','_padding'),getLayoutValue('sub','_padding'), getLayoutValue('sub','_border_size'),getLayoutValue('sub','_border_size'));
	cssString+=".menulist, .menulist  ul {\n";
	cssString+="\tmargin: 0;\n";
	cssString+="\tpadding: 0;\n";
	cssString+="\tlist-style: none;\n";
	cssString+="\tz-index: 10001;\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist{\n";
	cssString+="\twidth: "+getLayoutValue('main', '_width')+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul {\n";
	cssString+="\tdisplay: none;\n";
	cssString+="\tposition: absolute;\n";
	cssString+="\tleft: "+getLayoutValue('main', '_width')+";\n"; // maybe calculatiion + padding + border
	cssString+="\twidth: "+getLayoutValue('sub', '_width')+";\n";
	//cssString+="\ttop: -1px; margin-top: 0;\n";
	cssString+="\ttop:-"+getLayoutValue('sub','_distance')+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul ul{\n";
	cssString+="\tleft: "+getLayoutValue('sub', '_width')+";\n"; // maybe calculatiion + padding + border
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist li {\n";
	cssString+="\tdisplay: block;\n";
	cssString+="\tposition: relative;\n";
	//cssString+="\tborder: "+getLayoutValue('main','_border_size')+" "+getLayoutValue('main','_border_type')+" "+getLayoutValue('main','_border_color')+";\n";
	cssString+="\tmargin-top: "+layout_settings['main_distance']+";\n";
	//cssString+="\ttop:-"+layout_settings['menu_distance']+";\n";
	cssString+="}\n";
	cssString+=".menulist ul li {\n";
	cssString+="\tfloat: none;\n";
	cssString+="\tmargin-right: 0;\n";
	cssString+="\tmargin-bottom: -1px;\n";
	cssString+="\tmargin-top: "+layout_settings['sub_distance']+";\n";
	//cssString+="\tborder: "+getLayoutValue('sub','_border_size')+" "+getLayoutValue('sub','_border_type')+" "+getLayoutValue('sub','_border_color')+";\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul>li:last-child {\n";
	cssString+="\tmargin-bottom: 1px; /* Mozilla fix */\n";
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist a {\n";
	cssString+="\tdisplay: block;\n";
	//cssString+="\tborder: 0px;\n";
	cssString+=getLayoutPerModus('main');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul a {\n";
	cssString+="\tdisplay: block;\n";
	cssString+=getLayoutPerModus('sub');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist a:hover, .menulist a.highlighted:hover, .menulist a:focus {\n";
	cssString+=getLayoutPerModusCommon('activemain');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul a:hover, .menulist ul a.highlighted:hover, .menulist ul a:focus {\n";
	cssString+=getLayoutPerModusCommon('activesub');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist a.highlighted {\n";
	cssString+=getLayoutPerModusCommon('activemain');
	cssString+="}\n";
	cssString+=".menulist ul a.highlighted {\n";
	cssString+=getLayoutPerModusCommon('activesub');
	cssString+="}\n";
	cssString+="\n";
	if(layout_settings['menuCurrentStyle']){
		cssString+=".menulist a.menu-current-page {\n";
		cssString+="\tdisplay: block;\n";
		cssString+=getLayoutPerModus('currentmain');
		cssString+="}\n";
		cssString+=".menulist ul a.menu-current-page {\n";
		cssString+="\tdisplay: block;\n";
		cssString+=getLayoutPerModus('currentsub');
		cssString+="}\n";
	}
	cssString+="\n";
	cssString+=".menulist a .subind {\n";
	cssString+="\tfloat: right;\n";
	cssString+=getFontCss('main');
	cssString+="}\n";
	cssString+="\n";
	cssString+=".menulist ul a .subind {\n";
	cssString+="\tfloat: right;\n";
	cssString+=getFontCss('sub');
	cssString+="}\n";
	cssString+="\n";
	return cssString;
}
function stringFilter(s) {
	filteredValues = "1234567890-";
	var i;
	var returnString = "";
	var breaker=false;
	var hasnumber=false;
	for (i = s.length-1; i >= 0; i--) {
		var c = s.charAt(i);
		if(c==" "){
			if(hasnumber)break;
			else continue;
		}
		if (filteredValues.indexOf(c) == -1) returnString = c+returnString;
		else hasnumber=true;
	}
	return returnString;
}
function cssAdd(){
	var indication='';
	var total=0;
	for(var i=0; i<arguments.length; i++){
		number = parseInt(arguments[i]);
		if(!isNaN(number)){
			if(indication.length==0) indication=stringFilter(arguments[i]);
			total+=number;
		}
	}
	return total+indication;
}
function cssSubstractSpecial(first, second, type){
	/**
	 * This function substracts argument second from first. second can be multiple values (one two or four)
	 * Method depends on type
	 */
	second = trim(second);
	var vars=second.toString().split(' ');
	if(vars.length==1) return cssAdd(first, '-'+vars[0], '-'+vars[0]);
	if(vars.length==2 && type=='height') return cssAdd(first, '-'+vars[0], '-'+vars[0]);
	if(vars.length==2 && type=='width') return cssAdd(first, '-'+vars[1], '-'+vars[1]);
	if(vars.length==4 && type=='height') return cssAdd(first, '-'+vars[0], '-'+vars[2]);
	if(vars.length==4 && type=='width') return cssAdd(first, '-'+vars[1], '-'+vars[3]);
	return first;
}
function trim(value) {
	value = value.replace(/^s/,'');
	value = value.replace(/s$/,'');
   return value;
}
